package com.ruoyi.manage.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DESUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.AppUserMapper;
import com.ruoyi.manage.domain.AppUser;
import com.ruoyi.manage.service.IAppUserService;

/**
 * 小程序用户管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-03
 */
@Service
public class AppUserServiceImpl implements IAppUserService 
{
    /**
     * 数据加密密钥
     */
    @Value("${dataPassword}")
    private String dataPassword;

    @Autowired
    private AppUserMapper appUserMapper;

    /**
     * 查询小程序用户管理
     * 
     * @param id 小程序用户管理ID
     * @return 小程序用户管理
     */
    @Override
    public AppUser selectAppUserById(Long id)
    {
        AppUser appUser = appUserMapper.selectAppUserById(id);
        String phoneE = appUser.getPhone();
        String identitynumber = "";
        String phone = "";
        if(StringUtils.isNotEmpty(phoneE)){
            phone = DESUtil.decrypt(dataPassword, phoneE);
        }
        String identitynumberE = appUser.getIdentitynumber();
        if(StringUtils.isNotEmpty(identitynumberE)){
            identitynumber = DESUtil.decrypt(dataPassword, identitynumberE);
        }
        appUser.setPhone(phone);
        appUser.setIdentitynumber(identitynumber);
        return appUser;
    }

    /**
     * 查询小程序用户管理列表
     * 
     * @param appUser 小程序用户管理
     * @return 小程序用户管理
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser)
    {
        List<AppUser> appUsers = appUserMapper.selectAppUserList(appUser);
        for (AppUser user : appUsers) {
            String phoneE = user.getPhone();
            String identitynumber = "";
            String phone = "";
            if(StringUtils.isNotEmpty(phoneE)){
                phone = DESUtil.decrypt(dataPassword, phoneE);
            }
            String identitynumberE = user.getIdentitynumber();
            if(StringUtils.isNotEmpty(identitynumberE)){
                identitynumber = DESUtil.decrypt(dataPassword, identitynumberE);
            }
            user.setPhone(phone);
            user.setIdentitynumber(identitynumber);
        }
        return appUsers;
    }

    /**
     * 新增小程序用户管理
     * 
     * @param appUser 小程序用户管理
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUser appUser)
    {
        appUser.setCreateTime(DateUtils.getNowDate());
        return appUserMapper.insertAppUser(appUser);
    }

    /**
     * 修改小程序用户管理
     * 
     * @param appUser 小程序用户管理
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser)
    {
        AppUser appUser1 = appUserMapper.selectAppUserById(appUser.getId());
        String identitynumber = appUser1.getIdentitynumber();
        appUser.setIdentitynumber(identitynumber);
        String phone = appUser1.getPhone();
        appUser.setPhone(phone);
        return appUserMapper.updateAppUser(appUser);
    }


    /**
     * 批量删除小程序用户管理
     * 
     * @param ids 需要删除的小程序用户管理ID
     * @return 结果
     */
    @Override
    public int deleteAppUserByIds(Long[] ids)
    {
        return appUserMapper.deleteAppUserByIds(ids);
    }

    /**
     * 删除小程序用户管理信息
     * 
     * @param id 小程序用户管理ID
     * @return 结果
     */
    @Override
    public int deleteAppUserById(Long id)
    {
        return appUserMapper.deleteAppUserById(id);
    }


}
